package com.zxl.gtion.exception.advice;

import com.zxl.gtion.exception.BaseException;
import com.zxl.gtion.util.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 通用异常处理
 *
 * @Author: bo-feifei
 * @Date 2021/11/12 8:50
 * @Version 1.0
 */
@Slf4j
@ControllerAdvice
public class MyControllerAdvice {

    @ExceptionHandler(BaseException.class)
    @ResponseBody
    public R<?> handleBaseException(BaseException ex) {
//        log.warn("[业务异常] 出现业务异常：", ex);
        ex.printStackTrace();
        return R.fail(ex.getDefaultMessage());
    }

    @ExceptionHandler({AuthorizationException.class})
    @ResponseBody
    public R<?> exHandler(AuthorizationException e) {
//        log.error(e.getMessage());
        e.printStackTrace();
        return R.fail(e.getLocalizedMessage());
    }

    @ExceptionHandler({Exception.class})
    @ResponseBody
    public R<?> eHandler(Exception e) {
//        log.error(e.getMessage());
        e.printStackTrace();
        return R.fail("系统异常");
    }
}
